package tp.pr4.gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import tp.pr4.RobotEngine;
import tp.pr4.instructions.QuitInstruction;
import tp.pr4.instructions.UndoInstruction;

public class MainWindow extends JFrame {
	RobotEngine rbt;
	RobotPanel rbtPan;
	NavigationPanel centralPanel;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * Default constructor
	 */
	public MainWindow(RobotEngine robot){		
		super("WALL·E the garbage collector");
		this.rbt = robot;
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setPreferredSize(new Dimension(450,200));
		this.setMinimumSize(new Dimension(1050,800));
		init();
	}
	/**
	 * ends the game
	 */
	public void gameOver(){
		String message = "GAME OVER";
        String title = "GAME OVER";
        JOptionPane.showConfirmDialog(null, message, title, JOptionPane.OK_OPTION, JOptionPane.ERROR_MESSAGE);
        System.exit(0);
	}
	/**
	 * ends the game
	 */
	public void gameWon(){
		String message = "YOU HAVE REACHED YOUR SPACESHIP!";
        String title = "GAME OVER";
        JOptionPane.showConfirmDialog(null, message, title, JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE);
        System.exit(0);
	}	
	/**
	 * Asks the user if he really wants to exit
	 */
	public void quitGame(){
		String message = " Really Quit ? ";
        String title = "Quit";
        int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
        if (reply == JOptionPane.YES_OPTION){
			rbt.communicateRobot(new QuitInstruction());
			System.exit(0);
        }
		
	}
	/**
	 * Sets up the window layout
	 */
	private void init(){
		/*
		 * Creates the menu bar and its components
		 */
		JMenuBar menuBar = new JMenuBar();
		JMenu menu = new JMenu("File");
		JMenuItem quitItem = new JMenuItem("Quit");
		quitItem.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				quitGame();
			}
			
			
		});
		JMenuItem undoItem = new JMenuItem("Undo");
		undoItem.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				rbt.communicateRobot(new UndoInstruction());
			}
			
			
		});
		menuBar.add(menu);
		menu.add(quitItem);
		menu.add(undoItem);
		this.setJMenuBar(menuBar);
		/*
		 * Creates the main layout panel with borderlayout
		 */
		Container mainPanel = this.getContentPane();
		JPanel mainLayoutPanel = new JPanel();
		mainLayoutPanel.setLayout(new BorderLayout());	
			
		/*
		 * Creates the central component panel
		 */
		centralPanel = new NavigationPanel(rbt);
		
		/*
		 * Adds the 2 components of the splitpane
		 */
		rbtPan = new RobotPanel(this.rbt);
		InstructionsView ins = new InstructionsView(rbt, rbtPan,centralPanel);
		
		/*
		 * Creates the top split pane
		 */
		JSplitPane topPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,ins,rbtPan );
		mainLayoutPanel.add(topPanel,BorderLayout.NORTH);
		
		mainPanel.add(mainLayoutPanel); // adds the main pane into the window's pane
		
		mainLayoutPanel.add(centralPanel,BorderLayout.CENTER);
		
		this.pack();
		
	}	
	/**
	 * Refreshes the status of the window
	 */
	public void refresh(){
		rbtPan.refresh();		
		centralPanel.refresh();
	}
	/**
	 * returns the central navigation panel
	 * @return
	 */
	public NavigationPanel getNavigationPanel(){
		return this.centralPanel;
	}
}